package com.ayic.garbagerecycle.service.impl;

import com.ayic.garbagerecycle.mapper.RecyclingMapper;
import com.ayic.garbagerecycle.mapper.UserMapper;
import com.ayic.garbagerecycle.pojo.Result;

import com.ayic.garbagerecycle.pojo.User;
import com.ayic.garbagerecycle.service.RecyclingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class RecyclingServiceImpl implements RecyclingService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RecyclingMapper recyclingMapper;

    @Override
    public Result getRecyclingStatistics(String username) {
        // 根据用户名获取用户信息
        User user = userMapper.selectUserByName(username);
        if (user == null) {
            return Result.error("用户不存在");
        }

        // 获取用户今日回收量
        Integer todayRecycling = recyclingMapper.getTodayRecyclingByUserId(user.getUserId());
        // 获取用户本月回收量
        Integer monthRecycling = recyclingMapper.getMonthRecyclingByUserId(user.getUserId());

        // 封装返回数据
        Map<String, Integer> data = new HashMap<>();
        data.put("todayRecycling", todayRecycling != null ? todayRecycling : 0);
        data.put("monthRecycling", monthRecycling != null ? monthRecycling : 0);

        return Result.success(data);
    }

    @Override
    public Result getAllRecords() {
        return Result.success(recyclingMapper.selectAllRecords());
    }
}